import AntModal from "@/components/pt/dialog/AntModal";
import FileUpload from "@/components/pt/fileupload/IndexList";
import {
  getBizDocModel,
  addBizDocModel,
  updateBizDocModel,
  findMaxSort,
} from "@/api/biz/bizDocModel";
import SelectDept from "@/components/pt/selectDept/SelectDocType";
export default {
  name: "CreateForm",
  props: {},
  components: {
    AntModal,
    FileUpload,
    SelectDept,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加文件资料",
      // 表单参数
      form: {},
      rules: {},
      fileList: [],
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        docUrl: undefined,

        docType: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加文件资料";
      });
    },
    uploadCompleteFile(item, items) {
      console.log(("item", item, items));
      this.form.docUrl = item.url;
      const fileName = item.fileName;
      const fileExtension = fileName.split(".").pop();
      if (fileExtension === "xls" || fileExtension === "xlsx") {
        this.form.docType = "EXCEL";
      } else if (fileExtension === "doc" || fileExtension === "docx") {
        this.form.docType = "WORD";
      } else {
        this.form.docType = "UNDEF";
      }
      this.fileList = items;
      this.$message.success("上传成功", 3);
    },
    handlerRemoveFile(file) {
      this.$message.success("删除成功", 3);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizDocModelId = row.id;
      getBizDocModel(bizDocModelId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改文件资料";
        this.spinning = !this.spinning;
        this.fileList = [
          {
            name: this.form.docName,
            url: this.form.docUrl,
            status: "done",
            uid: this.form.id,
          },
        ];
        this.form.typeId = {
          ids: response.data.typeId,
          names: response.data.typeName,
        };
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const docType = this.form.typeId;
          this.form.typeName = docType.names;
          this.form.typeId = docType.ids;
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizDocModel(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizDocModel(saveForm).then((response) => {
              this.$message.success("新增成功", 3);

              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/docmodel/index";
      this.$router.push(index);
    },
  },
};
